home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / mach / hurd / __fork.c < prev    next >
C/C++ Source or Header  |  1994-06-05  |  14KB  |  418 lines

  1. /* Copyright (C) 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <hurd.h>
  22. #include <hurd/signal.h>
  23. #include <setjmp.h>
  24. #include "thread_state.h"
  25. #include <sysdep.h>        /* For stack growth direction.  */
  26. #include "set-hooks.h"
  27.  
  28. extern void _hurd_longjmp_thread_state (struct machine_thread_state *,
  29.                     jmp_buf env, int value);
  30.  
  31.  
  32. /* Things that want to be locked while forking.  */
  33. const struct
  34.   {
  35.     size_t n;
  36.     struct mutex *locks[0];
  37.   } _hurd_fork_locks;
  38.  
  39.  
  40. /* Things that want to be called before we fork, to prepare the parent for
  41.    task_create, when the new child task will inherit our address space.  */
  42. DEFINE_HOOK (_hurd_fork_prepare_hook, (void));
  43.  
  44. /* Things that want to be called when we are forking, with the above all
  45.    locked.  They are passed the task port of the child.  The child process
  46.    is all set up except for doing proc_child, and has no threads yet.  */
  47. DEFINE_HOOK (_hurd_fork_setup_hook, (void));
  48.  
  49. /* Things to be run in the child fork.  */
  50. DEFINE_HOOK (_hurd_fork_child_hook, (void));
  51.  
  52. /* Things to be run in the parent fork.  */
  53. DEFINE_HOOK (_hurd_fork_parent_hook, (void));
  54.  
  55.  
  56. /* Clone the calling process, creating an exact copy.
  57.    Return -1 for errors, 0 to the new process,
  58.    and the process ID of the new process to the old process.  */
  59. pid_t
  60. __fork (void)
  61. {
  62.   jmp_buf env;
  63.   pid_t pid;
  64.   size_t i;
  65.   error_t err;
  66.   thread_t thread_self = __mach_thread_self ();
  67.   struct hurd_sigstate *volatile ss;
  68.  
  69.   __mutex_lock (&_hurd_siglock);
  70.   for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
  71.     if (ss->thread == thread_self)
  72.       break;
  73.   if (ss)
  74.     ss->critical_section = 1;
  75.  
  76.   if (! setjmp (env))
  77.     {
  78.       process_t newproc;
  79.       task_t newtask;
  80.       thread_t thread, sigthread;
  81.       struct machine_thread_state state;
  82.       unsigned int statecount;
  83.       mach_port_t *portnames = NULL;
  84.       unsigned int nportnames = 0;
  85.       mach_port_type_t *porttypes = NULL;
  86.       unsigned int nporttypes = 0;
  87.       thread_t *threads = NULL;
  88.       unsigned int nthreads = 0;
  89.  
  90.       /* Run things that prepare for forking before we create the task.  */
  91.       RUN_HOOK (_hurd_fork_prepare_hook, ());
  92.  
  93.       /* Lock things that want to be locked before we fork.  */
  94.       for (i = 0; i < _hurd_fork_locks.n; ++i)
  95.     __mutex_lock (_hurd_fork_locks.locks[i]);
  96.       
  97.       newtask = MACH_PORT_NULL;
  98.       thread = sigthread = MACH_PORT_NULL;
  99.       newproc = MACH_PORT_NULL;
  100.  
  101.       /* Lock all the port cells for the standard ports while we copy the
  102.      address space.  We want to insert all the send rights into the
  103.      child with the same names.  */
  104.       for (i = 0; i < _hurd_nports; ++i)
  105.     __spin_lock (&_hurd_ports[i].lock);
  106.  
  107.       /* Create the child task.  It will inherit a copy of our memory.  */
  108.       if (err = __task_create (__mach_task_self (), 1, &newtask))
  109.     goto lose;
  110.  
  111.       /* Fetch the names of all ports used in this task.  */
  112.       if (err = __mach_port_names (__mach_task_self (),
  113.                    &portnames, &nportnames,
  114.                    &porttypes, &nporttypes))
  115.     goto lose;
  116.       if (nportnames != nporttypes)
  117.     {
  118.       err = EGRATUITOUS;
  119.       goto lose;
  120.     }
  121.  
  122.       /* Get send rights for all the threads in this task.
  123.      We want to avoid giving these rights to the child.  */
  124.       if (err = __task_threads (__mach_task_self (), &threads, &nthreads))
  125.     goto lose;
  126.  
  127.       /* Create the child main user thread and signal thread.  */
  128.       if ((err = __thread_create (newtask, &thread)) ||
  129.       (err = __thread_create (newtask, &sigthread)))
  130.     goto lose;
  131.  
  132.       /* Get the child process's proc server port.  We will insert it into
  133.      the child with the same name as we use for our own proc server
  134.      port; and we will need it to set the child's message port.  */
  135.       if (err = __proc_task2proc (_hurd_ports[INIT_PORT_PROC].port,
  136.                   newtask, &newproc))
  137.     goto lose;
  138.  
  139.       /* Insert all our port rights into the child task.  */
  140.       for (i = 0; i < nportnames; ++i)
  141.     {
  142.       if (MACH_PORT_TYPE (porttypes[i]) & MACH_PORT_TYPE_RECEIVE)
  143.         {
  144.           /* This is a receive right.  We want to give the child task
  145.          its own new receive right under the same name.  */
  146.           if (err = __mach_port_allocate_name (newtask,
  147.                            MACH_PORT_RIGHT_RECEIVE,
  148.                            portnames[i]))
  149.         goto lose;
  150.           if (MACH_PORT_TYPE (porttypes[i]) & MACH_PORT_TYPE_SEND)
  151.         {
  152.           /* Give the child as many send rights for its receive
  153.              right as we have for ours.  */
  154.           mach_port_urefs_t refs;
  155.           mach_port_t port;
  156.           mach_msg_type_name_t poly;
  157.           if (err = __mach_port_get_refs (__mach_task_self (),
  158.                           portnames[i],
  159.                           MACH_PORT_RIGHT_SEND,
  160.                           &refs))
  161.             goto lose;
  162.           if (err = __mach_port_extract_right (newtask,
  163.                                portnames[i],
  164.                                MACH_MSG_TYPE_MAKE_SEND,
  165.                                &port, &poly))
  166.             goto lose;
  167.           if (portnames[i] == _hurd_msgport)
  168.             {
  169.               /* We just created a receive right for the child's
  170.              message port and are about to insert send rights
  171.              for it.  Now, while we happen to have a send right
  172.              for it, give it to the proc server.  */
  173.               mach_port_t old;
  174.               if (err = __proc_setmsgport (newproc, port, &old))
  175.             goto lose;
  176.               if (old != MACH_PORT_NULL)
  177.             /* XXX what to do here? */
  178.             __mach_port_deallocate (__mach_task_self (), old);
  179.             }
  180.           if (err = __mach_port_insert_right (newtask,
  181.                               portnames[i],
  182.                               port,
  183.                               MACH_MSG_TYPE_MOVE_SEND))
  184.             goto lose;
  185.           if (refs > 1 &&
  186.               (err = __mach_port_mod_refs (newtask,
  187.                            portnames[i],
  188.                            MACH_PORT_RIGHT_SEND,
  189.                            refs - 1)))
  190.             goto lose;
  191.         }
  192.           if (MACH_PORT_TYPE (porttypes[i]) & MACH_PORT_TYPE_SEND_ONCE)
  193.         {
  194.           /* Give the child a send-once right for its receive right,
  195.              since we have one for ours.  */
  196.           mach_port_t port;
  197.           mach_msg_type_name_t poly;
  198.           if (err = __mach_port_extract_right
  199.               (newtask,
  200.                portnames[i],
  201.                MACH_MSG_TYPE_MAKE_SEND_ONCE,
  202.                &port, &poly))
  203.             goto lose;
  204.           if (err = __mach_port_insert_right
  205.               (newtask,
  206.                portnames[i], port,
  207.                MACH_MSG_TYPE_MOVE_SEND_ONCE))
  208.             goto lose;
  209.         }
  210.         }
  211.       else if (MACH_PORT_TYPE (porttypes[i]) &
  212.            (MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_DEAD_NAME))
  213.         {
  214.           /* This is a send right or a dead name.
  215.          Give the child as many references for it as we have.  */
  216.           mach_port_urefs_t refs;
  217.           mach_port_t insert;
  218.           if (portnames[i] == newtask)
  219.         /* Skip the name we use for the child's task port.  */
  220.         continue;
  221.           if (portnames[i] == __mach_task_self ())
  222.         /* For the name we use for our own task port,
  223.            insert the child's task port instead.  */
  224.         insert = newtask;
  225.           else if (portnames[i] == _hurd_ports[INIT_PORT_PROC].port)
  226.         {
  227.           /* Get the proc server port for the new task.  */
  228.           if (err = __USEPORT (PROC, __proc_task2proc (port, newtask,
  229.                                    &insert)))
  230.             goto lose;
  231.         }
  232.           else if (portnames[i] == thread_self)
  233.         /* For the name we use for our own thread port, insert the
  234.            thread port for the child main user thread.  We have one
  235.            extra user reference created at the beginning of this
  236.            function, accounted for by mach_port_names (and which
  237.            will thus be accounted for in the child below).  This
  238.            extra right gets consumed in the child by the store into
  239.            _hurd_sigthread in the child fork.  */
  240.         insert = thread;
  241.           else if (portnames[i] == _hurd_msgport_thread)
  242.         /* For the name we use for our signal thread's thread port,
  243.            insert the thread port for the child's signal thread.  */
  244.         insert = sigthread;
  245.           else
  246.         {
  247.           /* Skip the name we use for any of our own thread ports.  */
  248.           unsigned int j;
  249.           for (j = 0; j < nthreads; ++j)
  250.             if (portnames[i] == threads[j])
  251.               break;
  252.           if (j < nthreads)
  253.             continue;
  254.  
  255.           insert = portnames[i];
  256.         }
  257.           /* Find out how many user references we have for
  258.          the send right with this name.  */
  259.           if (err = __mach_port_get_refs (__mach_task_self (),
  260.                           portnames[i],
  261.                           MACH_PORT_RIGHT_SEND,
  262.                           &refs))
  263.         goto lose;
  264.           /* Insert the chosen send right into the child.  */
  265.           if (err = __mach_port_insert_right (newtask,
  266.                           portnames[i],
  267.                           insert,
  268.                           MACH_MSG_TYPE_COPY_SEND))
  269.         goto lose;
  270.           /* Give the child as many user references as we have.  */
  271.           if (refs > 1 &&
  272.           (err = __mach_port_mod_refs (newtask,
  273.                            portnames[i],
  274.                            MACH_PORT_RIGHT_SEND,
  275.                            refs - 1)))
  276.         goto lose;
  277.         }
  278.     }
  279.  
  280.       /* Unlock the standard port cells.  The child must unlock its own
  281.      copies too.  */
  282.       for (i = 0; i < _hurd_nports; ++i)
  283.     __spin_unlock (&_hurd_ports[i].lock);
  284.  
  285.  
  286.  
  287.       /* Register the child with the proc server.  */
  288.       if (err = __USEPORT (PROC, __proc_child (port, newtask)))
  289.     goto lose;
  290.  
  291.       /* Set the child signal thread up to run the msgport server function
  292.      using the same signal thread stack copied from our address space.
  293.      We fetch the state before longjmp'ing it so that miscellaneous
  294.      registers not affected by longjmp (such as i386 segment registers)
  295.      are in their normal default state.  */
  296.       statecount = MACHINE_THREAD_STATE_COUNT;
  297.       if (err = __thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
  298.                     (int *) &state, &statecount))
  299.     goto lose;
  300. #if STACK_GROWTH_UP
  301.       state.SP = __hurd_sigthread_stack_base;
  302. #else
  303.       state.SP = __hurd_sigthread_stack_end;
  304. #endif      
  305.       state.PC = (unsigned long int) _hurd_msgport_receive;
  306.       if (err = __thread_set_state (sigthread, MACHINE_THREAD_STATE_FLAVOR,
  307.                     (int *) &state, statecount))
  308.     goto lose;
  309.       /* We do not thread_resume SIGTHREAD here because the child
  310.      fork needs to do more setup before it can take signals.  */
  311.  
  312.       /* Set the child user thread up to return 1 from the setjmp above.  */
  313.       if (err = __thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
  314.                     (int *) &state, &statecount))
  315.     goto lose;
  316.       _hurd_longjmp_thread_state (&state, env, 1);
  317.       if (err = __thread_resume (thread))
  318.     goto lose;
  319.  
  320.       /* Get the PID of the child from the proc server.  */
  321.       err = __USEPORT (PROC, __proc_task2pid (port, newtask, &pid));
  322.  
  323.     lose:
  324.  
  325.       if (newtask != MACH_PORT_NULL)
  326.     {
  327.       if (err)
  328.         __task_terminate (newtask);
  329.       __mach_port_deallocate (__mach_task_self (), newtask);
  330.     }
  331.       if (thread != MACH_PORT_NULL)
  332.     __mach_port_deallocate (__mach_task_self (), thread);
  333.       if (sigthread != MACH_PORT_NULL)
  334.     __mach_port_deallocate (__mach_task_self (), sigthread);
  335.       if (newproc != MACH_PORT_NULL)
  336.     __mach_port_deallocate (__mach_task_self (), newproc);
  337.       if (thread_self != MACH_PORT_NULL)
  338.     __mach_port_deallocate (__mach_task_self (), thread_self);
  339.  
  340.       if (portnames)
  341.     __vm_deallocate (__mach_task_self (),
  342.              (vm_address_t) portnames,
  343.              nportnames * sizeof (*portnames));
  344.       if (porttypes)
  345.     __vm_deallocate (__mach_task_self (),
  346.              (vm_address_t) porttypes,
  347.              nporttypes * sizeof (*porttypes));
  348.       if (threads)
  349.     {
  350.       for (i = 0; i < nthreads; ++i)
  351.         __mach_port_deallocate (__mach_task_self (), threads[i]);
  352.       __vm_deallocate (__mach_task_self (),
  353.                (vm_address_t) threads,
  354.                nthreads * sizeof (*threads));
  355.     }
  356.  
  357.       /* Run things that want to run in the parent to restore it to
  358.      normality.  Usually prepare hooks and parent hooks are
  359.      symmetrical: the prepare hook arrests state in some way for the
  360.      fork, and the parent hook restores the state for the parent to
  361.      continue executing normally.  */
  362.       RUN_HOOK (_hurd_fork_parent_hook, ());
  363.     }
  364.   else
  365.     {
  366.       /* We are the child task.  Unlock the standard port cells, which were
  367.          locked in the parent when we copied its memory.  The parent has
  368.          inserted send rights with the names that were in the cells then.  */
  369.       for (i = 0; i < _hurd_nports; ++i)
  370.     __spin_unlock (&_hurd_ports[i].lock);
  371.  
  372.       /* We are the only thread in this new task, so we will
  373.      take the task-global signals.  */
  374.       _hurd_sigthread = thread_self;
  375.  
  376.       /* Free the sigstate structures for threads that existed in the
  377.      parent task but don't exist in this task (the child process).  */
  378.       while (_hurd_sigstates != NULL)
  379.     {
  380.       struct hurd_sigstate *next = _hurd_sigstates->next;
  381.       if (ss != _hurd_sigstates)
  382.         free (_hurd_sigstates);
  383.       _hurd_sigstates = next;
  384.     }
  385.       _hurd_sigstates = ss;
  386.       _hurd_sigstates->next = NULL;
  387.  
  388.       /* Fetch our various new process IDs from the proc server.  */
  389.       err = __USEPORT (PROC, __proc_getpids (port, &_hurd_pid, &_hurd_ppid,
  390.                          &_hurd_orphaned));
  391.       if (!err)
  392.     err = __USEPORT (PROC, __proc_getpgrp (port, _hurd_pid, &_hurd_pgrp));
  393.  
  394.       /* Run things that want to run in the child task to set up.  */
  395.       RUN_HOOK (_hurd_fork_child_hook, ());
  396.  
  397.       /* Set up proc server-assisted fault recovery for the signal thread.  */
  398.       _hurdsig_fault_init ();
  399.  
  400.       /* Start the signal thread listening on the message port.  */
  401.       if (!err)
  402.     err = __thread_resume (_hurd_msgport_thread);
  403.  
  404.       pid = 0;
  405.     }
  406.  
  407.   /* Unlock things we locked before creating the child task.
  408.      They are locked in both the parent and child tasks.  */
  409.   for (i = 0; i < _hurd_fork_locks.n; ++i)
  410.     __mutex_unlock (_hurd_fork_locks.locks[i]);
  411.  
  412.   if (ss)
  413.     ss->critical_section = 0;
  414.   __mutex_unlock (&_hurd_siglock);
  415.  
  416.   return err ? __hurd_fail (err) : pid;
  417. }
  418.